<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午12:03
 *
 * 食物类
 */

use LaravelBook\Ardent\Ardent;

class Carousel extends Ardent{

    public static $rules = array(
        'status' => 'numeric',
    );

    public static $relationsData = array(
        'thumbs' => array(self::MORPH_MANY, 'Thumb', 'name' => 'thumbable'),
    );

    const SHOW = 'SHOW';
    const HIDE = 'HIDE';

    public static $statuses = array(
        array('id' => 0,'keyword'=>'SHOW',   'display_name'=>'显示'),
        array('id' => 1,'keyword'=>'HIDE', 'display_name'=>'隐藏'),
    );

    const ADVERTISEMENT = 'ADVERTISEMENT';
    const USER_SETTLE_RANKING_LIST = 'USER_SETTLE_RANKING_LIST';
    const FOOD_ORDER_RANKING_LIST = 'FOOD_ORDER_RANKING_LIST';

    public static $types = array(
        array('id' => 0,'keyword'=>'ADVERTISEMENT',   'display_name'=>'广告'),
        array('id' => 1,'keyword'=>'USER_SETTLE_RANKING_LIST', 'display_name'=>'用户消费排行'),
        array('id' => 2,'keyword'=>'FOOD_ORDER_RANKING_LIST', 'display_name'=>'菜品下单量排行'),
    );

    public static $column = array(
        'status' => '状态',
        'type' => '类型',
        'description' => '描述',
        'updated_at' => '修改时间',
        'created_at' => '创建时间',
    );

    public static function boot()
    {
        parent::boot();

        self::saved(function($entry){

            $thumb_paths = Input::get( 'thumbs', '' );
            $entry->thumbs()->delete();
            if(is_array($thumb_paths)){
                foreach ($thumb_paths as $thumb_path){
                    $thumb = new Thumb();
                    $thumb->path = $thumb_path;
                    $entry->thumbs()->save($thumb);
                }
            }
        });
    }

    public static function getStatusDisplayNameByKeyword($keyword){
        foreach(self::$statuses as $status){
            if($status['keyword'] == $keyword){
                return $status['display_name'];
            }
        }
        return false;
    }

    public static function getTypeDisplayNameByKeyword($keyword){
        foreach(self::$types as $type){
            if($type['keyword'] == $keyword){
                return $type['display_name'];
            }
        }
        return false;
    }

    /**
     * 置顶
     * @return bool
     */
    public function goTop(){
        return $this->touch();
    }

}